package com.winit.openapi.lma.controller.builder;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.winit.oms.spi.lma.command.QueryShipmentOrderLogBySellerCommand;
import com.winit.oms.spi.lma.vo.LMAShipmentOrderImportLogSellerQueryVo;
import com.winit.oms.spi.lma.vo.LMAShipmentOrderImportLogVo;
import com.winit.oms.spi.lma.vo.LMAShipmentOrderMerchandiseImportLogVo;
import com.winit.oms.spi.lma.vo.LMAShipmentOrderPackageImportLogVo;
import com.winit.openapi.util.DateUtil;

/**
 * 导入日志信息查询
 * 
 * @version <pre>
 * Author	Version		Date		Changes
 * yu.zhang 	1.0  		2016-9-11 	Created
 * 
 * </pre>
 * @since 1.
 */
public class LMAShipmentOrderLogBuilder {

    /**
     * 构建查询日志的列表信息
     * 
     * @param json
     * @return
     */
    public static QueryShipmentOrderLogBySellerCommand buildQueryShipmentOrderLogBySeller(JSONObject json) {
        QueryShipmentOrderLogBySellerCommand command = new QueryShipmentOrderLogBySellerCommand();

        LMAShipmentOrderImportLogSellerQueryVo queryVo = new LMAShipmentOrderImportLogSellerQueryVo();
        if (json.containsKey("orderDateFrom")) {// winit跟踪号
            String orderDateStart = json.getString("orderDateFrom");
            orderDateStart += " 00:00:00";
            Date from = DateUtil.parse2Date(orderDateStart, DateUtil.FULL_DATE_STRING);
            queryVo.setOrderDateFrom(from);
        }
        if (json.containsKey("orderDateTo")) {// 结束时间
            String orderDateEnd = json.getString("orderDateTo");
            orderDateEnd += " 23:59:59";
            Date end = DateUtil.parse2Date(orderDateEnd, DateUtil.FULL_DATE_STRING);
            queryVo.setOrderDateTo(end);
        }
        if (json.containsKey("ids")) {
            JSONArray arr = json.getJSONArray("ids");
            List<Long> ids = new ArrayList<Long>();
            for (int i = 0; i < arr.size(); i++) {
                Long j = arr.getLong(i);
                ids.add(j);
            }
            queryVo.setIds(ids);
        }
        if (json.containsKey("sellerOrderNo")) {
            queryVo.setSellerOrderNo(json.getString("sellerOrderNo"));
        }
        if (json.containsKey("merchandiseCode")) {
            queryVo.setMerchandiseCode(json.getString("merchandiseCode"));
        }
        if (json.containsKey("winitProductCode")) {
            queryVo.setWinitProductCode(json.getString("winitProductCode"));
        }
        if (json.containsKey("importExcelName")) {
            queryVo.setImportExcelName(json.getString("importExcelName"));
        }
        command.setOrderLogVo(queryVo);
        return command;
    }

    /**
     * 查询导入日志信息
     * 
     * @param vos
     * @return
     */
    public static List<Map<String, Object>> buildGetOrderLogListBySeller(List<LMAShipmentOrderImportLogVo> vos) {
        List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
        for (LMAShipmentOrderImportLogVo vo : vos) {
            list.add(buildShipmentOrderLogListPageVo(vo));
        }
        return list;
    }

    /**
     * 构建查询列表的查询对象
     * 
     * @param vo
     * @return
     */
    public static Map<String, Object> buildShipmentOrderLogListPageVo(LMAShipmentOrderImportLogVo vo) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("id", vo.getOrderId());// ID
        map.put("sellerOrderNo", vo.getCustomerOrderNo());
        map.put("eBayOrderId", vo.getEbayOrderNo());
        map.put("eBaySellerId", vo.getEbaySellerId());
        map.put("eBayBuyerId", vo.getEbayBuyerId());
        map.put("orderTime", vo.getCreated());// 下单时间(订单创建时间)
        map.put("totalParcelsVolume", vo.getTotalParcelsVolume());
        map.put("totalParcelsWeight", vo.getTotalParcelsWeight());
        map.put("totalParcelsNumber", vo.getTotalParcelsNumber());
        map.put("customerName", vo.getCustomerName());
        map.put("countryCode", vo.getBuyerCountryOriginal());
        map.put("state", vo.getBuyerStateOriginal());
        map.put("city", vo.getBuyerCityOriginal());
        map.put("receiverName", vo.getBuyerNameOriginal());
        map.put("postCode", vo.getBuyerPostcodeOriginal());
        map.put("phoneNo", vo.getBuyerPhoneOriginal());
        map.put("email", vo.getBuyerEmailOriginal());
        map.put("houseNo", vo.getBuyerHouseNoOriginal());
        map.put("addressLine1", vo.getBuyerAddress1Original());
        map.put("addressLine2", vo.getBuyerAddress2Original());
        map.put("pickupAddressCode", vo.getDispatchWhCode());
        map.put("winitProductCode", vo.getWinitProductCode());
        map.put("winitProductName", vo.getWinitProductName());// Winit产品名称
        map.put("shiperMemo", vo.getShiperMemo());// Winit产品名称

        List<LMAShipmentOrderPackageImportLogVo> pkgVos = vo.getPackageLogList();
        if (pkgVos != null && pkgVos.size() > 0) {
            List<Map<String, Object>> pkgList = new ArrayList<Map<String, Object>>();
            for (LMAShipmentOrderPackageImportLogVo pkgVo : pkgVos) {
                Map<String, Object> pkgMap = new HashMap<String, Object>();
                pkgMap.put("parcelDesc", pkgVo.getParcelDesc());
                pkgMap.put("parcelNo", pkgVo.getParcelNo());
                pkgMap.put("weight", pkgVo.getWeight());
                pkgMap.put("length", pkgVo.getLength());
                pkgMap.put("width", pkgVo.getWidth());
                pkgMap.put("height", pkgVo.getHeight());
                pkgList.add(pkgMap);

                List<Map<String, Object>> merList = new ArrayList<Map<String, Object>>();
                List<LMAShipmentOrderMerchandiseImportLogVo> merVos = pkgVo.getMerchandiseLogList();
                if (merVos != null && merVos.size() > 0) {
                    for (LMAShipmentOrderMerchandiseImportLogVo merVo : merVos) {
                        Map<String, Object> merMap = new HashMap<String, Object>();
                        merMap.put("eBayTransactionId", merVo.geteBayTransactionId());
                        merMap.put("eBayItemId", merVo.geteBayItemId());
                        merMap.put("merchandiseName", merVo.getMerchandiseName());
                        merMap.put("merchandiseCode", merVo.getMerchandiseCode());
                        merMap.put("isHasBattery", merVo.getIsHasBattery());
                        merMap.put("quantity", merVo.getQuantity());
                        merMap.put("specifications", merVo.getSpecifications());
                        merMap.put("salePrice", merVo.getSalePrice());
                        merMap.put("saleCurrency", merVo.getSaleCurrency());
                        merMap.put("declaredValue", merVo.getDeclaredValue());
                        merMap.put("declaredCurrency", merVo.getDeclaredCurrency());
                        merMap.put("declaredNameCn", merVo.getDeclaredNameCn());
                        merMap.put("declaredNameEn", merVo.getDeclaredNameEn());
                        merList.add(merMap);
                    }
                }
                pkgMap.put("merchandiseList", merList);
            }
            map.put("parcelList", pkgList);
        }
        return map;
    }
}
